package com.mdp.im.core.service.admin;

import com.mdp.core.api.CacheHKVService;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;
@Service
public class BlackUserFrendsService {

	public static String prefix="im_black_user_frends_";

	public static CacheHKVService redisTemplate;

	@Autowired
	public  void setRedisTemplate(CacheHKVService redisTemplate) {
		BlackUserFrendsService.redisTemplate = redisTemplate;
	}
	
	static public boolean isBlack(String myUserid,String blackUserid){
		Set<String> blackUserids=getBlackUsers(myUserid);
		if(blackUserids==null) {
			return false;
		}else {
			return blackUserids.contains(blackUserid);
		}
		
	}
	
	static public void putBlack(String myUserid,String blackUserid){
		Set<String> blackUserids=getBlackUsers(myUserid);
		if(blackUserids==null) {
			blackUserids=new HashSet<>(); 
			blackUserids.add(blackUserid);
			putBlackUsers(myUserid,blackUserids);
		}else {
			blackUserids.add(blackUserid);
			putBlackUsers(myUserid,blackUserids);
		}
		
	}
	static public void removeBlack(String myUserid,String blackUserid){
		Set<String> blackUserids=getBlackUsers(myUserid);
		if(blackUserids!=null) { 
			blackUserids.remove(blackUserid);
			putBlackUsers(myUserid,blackUserids);
		} 
		
	}

	public static Set<String> getBlackUsers(String myUserid){
		return (Set<String>) redisTemplate.get(prefix+myUserid,myUserid);
	}

	public static void putBlackUser(String myUserid,String blackUserid){
		Set<String> ga=getBlackUsers(myUserid);
		ga.add(blackUserid);
		putBlackUsers(myUserid,ga);
	}

	public static void putBlackUsers(String myUserid,Set<String> blackUsers){
		redisTemplate.put(prefix+myUserid,myUserid,blackUsers);
	}
}
